/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.HashMap;

/**
 * ViewHolder
 *
 * @since 2021-03-24
 */
class ViewHolder {
    int position;
    int layoutId;
    private Component component;
    private Context context;
    private HashMap<Integer, Component> views;

    /**
     * 构造函数
     *
     * @param context 参数
     * @param itemView 参数
     * @param parent 参数
     * @param position 参数
     */
    ViewHolder(Context context, Component itemView, ComponentContainer parent, int position) {
        this.context = context;
        this.component = itemView;
        this.position = position;
        views = new HashMap<>(0);
        component.setTag(this);
    }

    /**
     * getView
     *
     * @param viewId 参数
     * @param <T> 参数
     * @return T
     */
    public <T extends Component> T getView(int viewId) {
        Component view = views.get(viewId);
        if (view == null) {
            view = component.findComponentById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    Component getComponentView() {
        return component;
    }

    /**
     * setText
     *
     * @param viewId 参数
     * @param text 参数
     */
    public void setText(int viewId, String text) {
        Text tv = getView(viewId);
        tv.setText(text);
    }

    /**
     * setImageResource
     *
     * @param viewId 参数
     * @param resId 参数
     */
    public void setImageResource(int viewId, int resId) {
        Image image = getView(viewId);
        image.setPixelMap(resId);
        image.setScaleMode(Image.ScaleMode.STRETCH);
    }
}
